<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\UserCharmRichLog;
use App\Model\UserProfile;
use App\Model\UserRankDay;
use App\Model\UserRankMon;
use App\Model\UserRankTotal;
use Carbon\Carbon;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\DbConnection\Db;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
 * @Command
 */
class CronTask extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('cron');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }


    protected function getArguments()
    {
        return [
            ['method', InputArgument::OPTIONAL, '方法', null],
            ['args', InputArgument::IS_ARRAY, '参数', null],
        ];
    }

    public function handle()
    {
        // Db::enableQueryLog();
        $method = $this->input->getArgument('method');
        $args = $this->input->getArgument('args');

	//echo '<pre>';var_dump($this->input);
        if (method_exists($this, $method)) {
            $begin = Carbon::now();
            $this->{$method}(...$args);
            $ms = Carbon::now()->diffInMilliseconds($begin);
            $this->info("processed $method in {$ms}ms");
        }
        // print_r(Db::getQueryLog());
        // echo PHP_EOL;
    }

    /**
     * 日榜
     * php bin/hyperf.php cron dailyRank 1
     */
    function dailyRank($param = 1)
    {
        $limit = 20;
        $start = date('Y-m-d'); //date('Y-m-d', strtotime('-1 day'));
        $end = date('Y-m-d', strtotime('+1 day')); //date('Y-m-d');
        //魅力榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'charm']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $limit) break;
            $rank = $idx + 1;
            UserRankDay::addCharmRecord($item['user_id'], $rank, $item['value'], $start);
            log_info(__CLASS__ . "==dailyRank==UserId:{$item['user_id']}==Charm:{$item['charm_value']}==Rank:{$rank}");
        }

        //豪气榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'rich']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $limit) break;
            UserRankDay::addRichRecord($item['user_id'], $idx + 1, $item['value'], $start);
            log_info(__CLASS__ . "==dailyRank==UserId:{$item['user_id']}==Rich:{$item['rich_value']}==Rank:{$rank}");
        }
        return;
    }

    /**
     * 月榜
     * php bin/hyperf.php cron monthRank 1
     */
    function monthRank($param = 1)
    {
        $limit = 20;
        $start = date('Y-m-d', strtotime('-30 day'));
        $end = date('Y-m-d', strtotime('+1 day'));

        //清除记录
        $sql = "TRUNCATE t_user_rank_mon";
        Db::statement($sql);

        //魅力榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'charm']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        // var_dump($where);
        // return var_dump($items);
        foreach ($items as $idx => $item) {
            if ($idx >= $limit) break;
            $rank = $idx + 1;
            UserRankMon::addCharmRecord($item['user_id'], $rank, $item['value']);
            log_info(__CLASS__ . "==monthRank==UserId:{$item['user_id']}==Charm:{$item['charm_value']}==Rank:{$rank}");
        }

        //豪气榜
        $where = [
            ['created_at', '<', $end],
            ['created_at', '>', $start],
            ['type', 'rich']
        ];
        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $limit) break;
            UserRankMon::addRichRecord($item['user_id'], $idx + 1, $item['value']);
            log_info(__CLASS__ . "==monthRank==UserId:{$item['user_id']}==Rich:{$item['rich_value']}==Rank:{$rank}");
        }
        return;
    }

    /**
     * 总榜
     * php bin/hyperf.php cron totalRank 1
     */
    function totalRank($param = 1)
    {
        $limit = 20;

        //清除记录
        $sql = "TRUNCATE t_user_rank_total";
        Db::statement($sql);

        //魅力榜
        $where = [['type', 'charm'], ['flag', 1]];

        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);

        foreach ($items as $idx => $item) {
            if ($idx >= $limit) break;
            $rank = $idx + 1;
            UserRankTotal::addCharmRecord($item['user_id'], $rank, $item['value']);
            log_info(__CLASS__ . "==totalRank==UserId:{$item['user_id']}==Charm:{$item['charm_value']}==Rank:{$rank}");
        }

        //豪气榜
        $where = [['type', 'rich'], ['flag', 1]];

        $items = UserCharmRichLog::where($where)->select(['user_id', Db::raw('SUM(value) as value')])->groupBy('user_id')->get()->toArray();
        array_multisort(array_column($items, 'value'), SORT_DESC, $items);
        foreach ($items as $idx => $item) {
            if ($idx >= $limit) break;
            UserRankTotal::addRichRecord($item['user_id'], $idx + 1, $item['value']);
            log_info(__CLASS__ . "==totalRank==UserId:{$item['user_id']}==Rich:{$item['rich_value']}==Rank:{$rank}");
        }
        return;
    }
}
